package com.tz;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * Created by user on 2016/10/10.
 */
@Configuration
@ComponentScan({"com.tz.service","com.tz.dao"})
@EnableAspectJAutoProxy
@EnableTransactionManagement
public class AppConfig {

    @Bean
    public DataSource getDateSource(){
        BasicDataSource bds=new BasicDataSource();

        bds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        bds.setUrl("jdbc:oracle:thin:@localhost:1521:XE");
        bds.setUsername("jsd1606");
        bds.setPassword("jsd1606");
        bds.setInitialSize(8);

        return bds;
    }

    @Bean
    public LocalSessionFactoryBean getSessionFactory(){
        LocalSessionFactoryBean lsfb=new LocalSessionFactoryBean();

        lsfb.setDataSource(getDateSource());

        Properties props=new Properties();
        props.setProperty("hibernate.dialect","org.hibernate.dialect.Oracle10gDialect");
        props.setProperty("hibernate.show_sql","true");
        lsfb.setHibernateProperties(props);

        lsfb.setPackagesToScan("com.tz.entity");
        return lsfb;
    }

    @Bean
    public HibernateTransactionManager getTM(){
        HibernateTransactionManager htm=new HibernateTransactionManager(getSessionFactory().getObject());
        return htm;

    }
}
